package org.aaron.demos;

import java.util.Set;

import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class BlueToothActivity extends ListActivity {
	private BluetoothAdapter bluetoothAdapter;
	private ArrayAdapter<String> adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bluetoothlayout);
		initActivity();
	}

	private void initActivity() {

		Button openBtn = (Button) findViewById(R.id.openBtn);
		Button findDervice = (Button) findViewById(R.id.findDevicesBtn);
		Button allowFindBtn = (Button) findViewById(R.id.allowFindBtn);
		Button findBlueBtn = (Button) findViewById(R.id.findBlueBtn);

		openBtn.setOnClickListener(new OpenClickListener());
		findDervice.setOnClickListener(new FindDerviceClickListener());
		allowFindBtn.setOnClickListener(new AllowFindClickListener());
		findBlueBtn.setOnClickListener(new FindDevicesClickListener());
		IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		BlueToothReceiver blueToothReceiver = new BlueToothReceiver();
		registerReceiver(blueToothReceiver, intentFilter);
		adapter=new ArrayAdapter<String>(BlueToothActivity.this, android.R.layout.simple_list_item_1);
		setListAdapter(adapter);

	}

	class OpenClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
			if (bluetoothAdapter != null) {
				Log.d("BlueToothActivity OpenClickListener", "找到蓝牙适配器");
				if (!bluetoothAdapter.isEnabled()) {
					Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
					startActivityForResult(intent, 0);

				}
			} else {
				Log.d("BlueToothActivity OpenClickListener", "没有蓝牙适配器");
			}
		}
	}

	class FindDerviceClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
			if (pairedDevices.size() > 0) {
				for (BluetoothDevice device : pairedDevices) {
					adapter.add(device.getName() + "\n" + device.getAddress());
				}
			
			}
		}
	}

	class AllowFindClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
			intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 100);
			startActivity(intent);
		}
	}

	class FindDevicesClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			bluetoothAdapter.startDiscovery();
		}

	}

	class BlueToothReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
				BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				Log.d("BlueToothActivity BlueToothReceiver ", bluetoothDevice.getAddress());
			}
		}
	}
}
